/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.account.core.service;


import info.peigen.hotpot.business.account.facade.AccountServiceFacade;
import info.peigen.hotpot.business.account.facade.order.create.*;
import info.peigen.hotpot.business.account.facade.order.edit.EditAccountOrder;
import info.peigen.hotpot.business.account.facade.order.freeze.FreezeAccountOrder;
import info.peigen.hotpot.business.account.facade.order.freeze.UnfreezeAccountOrder;
import info.peigen.hotpot.business.account.facade.result.create.CreateAccountResult;
import info.peigen.hotpot.business.account.facade.result.edit.EditAccountResult;
import info.peigen.hotpot.business.account.facade.result.freeze.FreezeAccountResult;
import info.peigen.hotpot.business.account.facade.result.freeze.UnfreezeAccountResult;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceInvoke;
import org.noear.solon.annotation.Component;


/**
 * <b>(AccountService)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/7/29
 */
@Component
public class AccountService extends AbstractAccountService implements AccountServiceFacade {

    @Override
    public CreateAccountResult createCustomerAccount(CreateCustomerAccountOrder order) {
        ProcessorServiceContext serviceContext = ProcessorServiceContext.builder().order(order).result(new CreateAccountResult()).build();
        execute(serviceContext, new ProcessorServiceInvoke() {
            @Override
            public void addProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(createCustomerAccountProcessor);
            }

            @Override
            public void addPostProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(storeCreateAccountProcessor, createAccountResultProcessor);
            }
        });
        return (CreateAccountResult) serviceContext.getResult();
    }

    @Override
    public CreateAccountResult createProvisionsAccount(CreateProvisionsAccountOrder order) {
        ProcessorServiceContext serviceContext = ProcessorServiceContext.builder().order(order).result(new CreateAccountResult()).build();
        execute(serviceContext, new ProcessorServiceInvoke() {
            @Override
            public void addProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(createProvisionsAccountProcessor);
            }

            @Override
            public void addPostProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(storeCreateAccountProcessor, createAccountResultProcessor);
            }
        });
        return (CreateAccountResult) serviceContext.getResult();
    }

    @Override
    public CreateAccountResult createFeeAccount(CreateFeeAccountOrder order) {
        ProcessorServiceContext serviceContext = ProcessorServiceContext.builder().order(order).result(new CreateAccountResult()).build();
        execute(serviceContext, new ProcessorServiceInvoke() {
            @Override
            public void addProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(createFeeAccountProcessor);
            }

            @Override
            public void addPostProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(storeCreateAccountProcessor, createAccountResultProcessor);
            }
        });
        return (CreateAccountResult) serviceContext.getResult();
    }

    @Override
    public CreateAccountResult createIntermediateAccount(CreateIntermediateAccountOrder order) {
        ProcessorServiceContext serviceContext = ProcessorServiceContext.builder().order(order).result(new CreateAccountResult()).build();
        execute(serviceContext, new ProcessorServiceInvoke() {
            @Override
            public void addProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(createIntermediateAccountProcessor);
            }

            @Override
            public void addPostProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(storeCreateAccountProcessor, createAccountResultProcessor);
            }
        });
        return (CreateAccountResult) serviceContext.getResult();
    }

    @Override
    public CreateAccountResult createPartnerAccount(CreatePartnerAccountOrder order) {
        ProcessorServiceContext serviceContext = ProcessorServiceContext.builder().order(order).result(new CreateAccountResult()).build();
        execute(serviceContext, new ProcessorServiceInvoke() {
            @Override
            public void addProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(createPartnerAccountProcessor);
            }

            @Override
            public void addPostProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(storeCreateAccountProcessor, createAccountResultProcessor);
            }
        });
        return (CreateAccountResult) serviceContext.getResult();
    }

    @Override
    public EditAccountResult editAccount(EditAccountOrder order) {
        ProcessorServiceContext serviceContext = ProcessorServiceContext.builder().order(order).result(new EditAccountResult()).build();
        execute(serviceContext, new ProcessorServiceInvoke() {
            @Override
            public void addPreProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(activeAccountProcessor);
            }

            @Override
            public void addProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(editAccountProcessor, editAccountResultProcessor);
            }

            @Override
            public void addPostProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(storeAccountProcessor);
            }
        });
        return (EditAccountResult) serviceContext.getResult();
    }

    @Override
    public FreezeAccountResult freezeAccount(FreezeAccountOrder order) {
        ProcessorServiceContext serviceContext = ProcessorServiceContext.builder().order(order).result(new FreezeAccountResult()).build();
        execute(serviceContext, new ProcessorServiceInvoke() {
            @Override
            public void addPreProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(activeAccountProcessor);
            }

            @Override
            public void addProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(freezeAccountProcessor);
            }

            @Override
            public void addPostProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(storeAccountProcessor);
            }
        });
        return (FreezeAccountResult) serviceContext.getResult();
    }

    @Override
    public UnfreezeAccountResult unfreezeAccount(UnfreezeAccountOrder order) {
        ProcessorServiceContext serviceContext = ProcessorServiceContext.builder().order(order).result(new UnfreezeAccountResult()).build();
        execute(serviceContext, new ProcessorServiceInvoke() {
            @Override
            public void addPreProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(activeAccountForUnfreezeProcessor);
            }

            @Override
            public void addProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(unfreezeAccountProcessor);
            }

            @Override
            public void addPostProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(storeAccountProcessor);
            }
        });
        return (UnfreezeAccountResult) serviceContext.getResult();
    }
}
